package com.hxut.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import java.io.IOException;
import java.io.PrintWriter;

@WebFilter(filterName = "MyFilter",urlPatterns = "/testServlet02",initParams = {@WebInitParam(name = "Encoding",value = "utf-8")})
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("MyFilter init()");
        //通过filterConfig获取初始化参数
        String value = filterConfig.getInitParameter("encoding");
        System.out.println(value);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        PrintWriter out = servletResponse.getWriter();
        out.println("hello filter");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("MyFilter destroy()");
    }
}
